#include "Input.h"

/*The constructor we want to run*/
Input::Input(HINSTANCE hInst, HWND hWnd)
{
	/*Clear out all of our attributes*/
	ZeroMemory(&m_KeyboardState, sizeof(m_KeyboardState));
	ZeroMemory(&m_KeyDown, sizeof(m_KeyDown));
	ZeroMemory(&m_MouseState, sizeof(m_MouseState));
	m_InputDevice = 0;
	m_Keyboard = 0;
	m_Mouse = 0;

	/*Now since this is the constructor we need to create our input object*/
	DirectInput8Create(hInst,						//Handle to our Instance
		DIRECTINPUT_VERSION,			//The version of direct input we are using
		IID_IDirectInput8,			//The type of device
		(void**)&m_InputDevice,		//The input device pointer
		0);							//The punkOuter....

	/*Now that we have the object now we create each device*/
	m_InputDevice->CreateDevice(GUID_SysKeyboard,	//The type of device
		&m_Keyboard,		//The pointer to the device
		0);					//LPUNKNOWN

	m_InputDevice->CreateDevice(GUID_SysMouse,		//The type of device
		&m_Mouse,			//The pointer to the device
		0);					//LPUNKNOWN

	/*Set the data formats for each device*/
	m_Keyboard->SetDataFormat(&c_dfDIKeyboard);

	m_Mouse->SetDataFormat(&c_dfDIMouse2);

	/*Set the cooperative Level of the devices*/
	m_Keyboard->SetCooperativeLevel(hWnd,			//The handle to our window
		DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);	//The cooperative flags

	m_Mouse->SetCooperativeLevel(hWnd,				//The handle to our window
		DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);//The cooperative flags

	/*Acquire the Devices*/
	m_Keyboard->Acquire();

	m_Mouse->Acquire();

}

/*The default destructor*/
Input::~Input(void)
{
	/*If the device isn't null*/
	if(m_InputDevice) 
	{
		/*Release the device*/
		m_InputDevice->Release();

		/*Make the pointer null*/
		m_InputDevice = 0;
	}

	/*If the device isn't null*/
	if(m_Keyboard) 
	{
		/*We must unacquire the devices before releasing them*/
		m_Keyboard->Unacquire();

		/*Release the device*/
		m_Keyboard->Release();

		/*Make the pointer null*/
		m_Keyboard = 0;
	}

	/*If the device isn't null*/
	if(m_Mouse) 
	{
		/*We must unacquire the devices before releasing them*/
		m_Mouse->Unacquire();

		/*Release the device*/
		m_Mouse->Release();

		/*Make the pointer null*/
		m_Mouse = 0;
	}
}

/*The function to acquire the input*/
void Input::acquire()
{
	/*Acquire the devices again*/
	m_Keyboard->Acquire();
	m_Mouse->Acquire();

	/*Get the device state of the Keyboard*/
	m_Keyboard->GetDeviceState(sizeof(m_KeyboardState), (void**)&m_KeyboardState);

	/*Get the device state of the Mouse*/
	m_Mouse->GetDeviceState(sizeof(m_MouseState), (void**)&m_MouseState);
}

/*The function to check if the sent in key was pressed*/
bool Input::keyDown(unsigned char key)
{
	/*If it was pressed*/
	if(m_KeyboardState[key] & 0x80)
	{
		/*Return True*/
		return true;
	}

	/*If it wasn't return false*/
	return false;
}

/*The function to check if the sent in button was pressed*/
bool Input::mouseDown(int button)
{
	/*If it was pressed*/
	if(m_MouseState.rgbButtons[button] & 0x80)
	{
		/*Return True*/
		return true;
	}

	/*If it wasn't return false*/
	return false;
}

/*The function to return the mouse difference in the X direction*/
float Input::mouseX()
{
	return (float)m_MouseState.lX;
}

/*The function to return the mouse difference in the Y direction*/
float Input::mouseY()
{
	return (float)m_MouseState.lY;
}

/*The function to return the mouse difference in the Z direction*/
float Input::mouseZ()
{
	return (float)m_MouseState.lZ;
}

/*The function to return the pointer to the bool array*/
bool* Input::getBoolArray()
{
	return m_KeyDown;
}